package com.hnucm.eatwhat_java.adapter;

import android.content.Context;
import android.provider.SyncStateContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.hnucm.eatwhat_java.R;
import com.hnucm.eatwhat_java.dao.ShopDao_Impl;
import com.hnucm.eatwhat_java.databinding.ShopItemBinding;
import com.hnucm.eatwhat_java.pojo.Shop;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.List;

public class ShopAdapter extends SwipeRecyclerView.Adapter<ShopAdapter.MyViewHolder> {
    private String TAG="MainActivity";
    private List<Shop> shopList;
    private Context context;
    private final ImageLoader imageLoader=ImageLoader.getInstance();

    private DisplayImageOptions options=new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.nulldata)          // 设置图片下载期间显示的图片
            .showImageForEmptyUri(R.drawable.nulldata)  // 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.drawable.nulldata)       // 设置图片加载或解码过程中发生错误显示的图片
            .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
            .displayer(new RoundedBitmapDisplayer(20))  // 设置成圆角图片
            .build();                                   // 创建配置过得DisplayImageOption对象;
    public ShopAdapter(List<Shop> shopList, Context context) {
        this.shopList = shopList;
        this.context = context;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=LayoutInflater.from(context).inflate(R.layout.shop_item,parent, false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Glide.with(holder.imageView).load(shopList.get(position).getShopPic()).into(holder.imageView);
        holder.textView.setText(shopList.get(position).getShopName());
    }


    @Override
    public int getItemCount() {
        return shopList==null?0:shopList.size();
    }

    static class MyViewHolder extends SwipeRecyclerView.ViewHolder{

        private TextView textView;
        private ImageView imageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.shop_name);
            imageView=itemView.findViewById(R.id.shop_img);
        }
    }
}
